package com.free.chat.web;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.free.chat.entity.ApiResult;
import com.free.chat.entity.model.Users;
import com.free.chat.service.UsersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.UploadedFile;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

@Valid
@Controller
@Mapping("users")
@Api(tags = "用户管理")
public class UsersController {
    @Inject
    private UsersService usersService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Get
    @Mapping("queryById")
    public ApiResult queryById(Long id) {
        return ApiResult.success(this.usersService.queryById(id));
    }

    @Get
    @Mapping("list")
    public ApiResult list(Users users) {
        return usersService.list(users);
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @Post
    @Mapping("add")
    public ApiResult add(Users users) {
        return ApiResult.success(this.usersService.insert(users));
    }

    /**
     * 编辑数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @Post
    @Mapping("edit")
    public ApiResult edit(Users users) {
        return ApiResult.success(this.usersService.update(users));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Get
    @Mapping("deleteById")
    public ApiResult deleteById(Long id) {
        return ApiResult.success(this.usersService.deleteById(id));
    }

    /**
     * 登录接口，后面会用sa-token重新优化，先手写一个~
     *
     * @param users 用户
     */
    @Post
    @Mapping("login")
    @ApiOperation(value = "用户登录")
    public ApiResult login(Users users) {
        return usersService.login(users);
    }

    /**
     * 注册用户
     *
     * @param user 用户
     */
    @Post
    @Mapping("register")
    @ApiOperation(value = "用户注册")
    public ApiResult register(@Validated Users user) {
        return usersService.register(user);
    }

    /**
     * 注册用户
     */
    @Post
    @Mapping("logout")
    @ApiOperation(value = "用户退出")
    public ApiResult logout() {
        // 清空session
        StpUtil.logout();
        return ApiResult.success();
    }

    /**
     * 获取好友列表
     */
    @Get
    @Mapping("getCurrUserFriends")
    @SaCheckLogin
    public ApiResult getCurrUserFriends() {
        Long id = StpUtil.getLoginIdAsLong();
        return ApiResult.success(usersService.getCurrUserFriends(id));
    }

    /**
     * @Author: 朝花不迟暮
     * @Description: 头像上传
     * @Date: 2022/11/11
     * @Param: file 头像文件
     */
    @Post
    @Mapping("/uploadImg")
    public ApiResult uploadImg(UploadedFile file) {
        return usersService.uploadImg(file);
    }

    /**
     * @Description: 该方法为一个测试接口
     * @Param:
     * @Author: 朝花不迟暮
     * @Date: 2022/11/12
     */
    @Get
    @Mapping("/test")
    @SaCheckLogin
    public ApiResult test() {
        return ApiResult.success();
    }
}

